package com.iotechn.unimall.app.api.news;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.iotechn.unimall.app.api.enterprise.EnterpriseServiceImpl;
import com.iotechn.unimall.core.annotation.HttpMethod;
import com.iotechn.unimall.core.annotation.HttpParam;
import com.iotechn.unimall.core.annotation.HttpParamType;
import com.iotechn.unimall.core.exception.AdminServiceException;
import com.iotechn.unimall.core.exception.ExceptionDefinition;
import com.iotechn.unimall.core.exception.ServiceException;
import com.iotechn.unimall.data.domain.NewsDO;
import com.iotechn.unimall.data.domain.SettingsImageDO;
import com.iotechn.unimall.data.mapper.NewsMapper;
import com.iotechn.unimall.data.mapper.SettingsImageMapper;
import com.iotechn.unimall.data.model.Page;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class NewsServiceImpl implements NewsService {

    @Autowired
    private NewsMapper newsMapper;
    @Autowired
    SettingsImageMapper settingsImageMapper;

    @Override
    public List<NewsDO> home( String categoryId
            ){
        Wrapper<NewsDO> wrapper = new EntityWrapper<NewsDO>();
        wrapper.eq("category",categoryId).eq("is_use",1).setSqlSelect("id","title","banner","intro")
                .orderBy("id",false)
                .last(" limit 1");
        List<NewsDO> list = newsMapper.selectList(wrapper);
        list.addAll(newsMapper.selectList(wrapper.setSqlSelect("id","title")
                .orderBy("id",false)
                .last(" limit 1,5")));
        return list;
    }

    @Override
    public String roundRobinBanner(){
        Wrapper<SettingsImageDO> imageWrapper = new EntityWrapper<SettingsImageDO>();
        imageWrapper.setSqlSelect("image_url","url","location")
                .eq("location", EnterpriseServiceImpl.IMAGE_TYPE.NEWS_BANNER.CODE).orderBy("`order`", false);
        List<SettingsImageDO> bannerList = settingsImageMapper.selectList(imageWrapper);
        JSONArray array = new JSONArray();
        bannerList.forEach(i -> {
            JSONObject json = new JSONObject();
           /* json.put("image",i.getImageUrl());
            json.put("link",i.getUrl());*/
            array.add(json);
        });
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("banner",array.toJSONString());
        return jsonObject.toJSONString();
    }

    @Override
    public Page<NewsDO> list(String categoryId,Integer page, Integer limit){
        Wrapper<NewsDO> wrapper = new EntityWrapper<NewsDO>().eq("is_use",1)
                .orderBy("id", false);
        Integer count = newsMapper.selectCount(wrapper);
        List<NewsDO> NewsDOS = newsMapper.selectPage(new RowBounds((page - 1) * limit, limit), wrapper);
        return new Page<>(NewsDOS, page, limit, count);
    }



    @Override
    public List<NewsDO> yifentianList(){
        Wrapper<NewsDO> wrapper = new EntityWrapper<NewsDO>().in("category","7,8,9")
                .setSqlSelect("id","title","banner","`intro`","gmt_update","gmt_create")
                .orderBy("id", false);
        List<NewsDO> newsDOS = newsMapper.selectPage(new RowBounds(0, 2),wrapper);
        return newsDOS;
    }

    @Override
    public NewsDO detail(String id) throws ServiceException {
        NewsDO newsDO = newsMapper.selectById(id);
        if (newsDO != null)
            return newsDO;
        throw new AdminServiceException(ExceptionDefinition.NEWS_NOT_EXIST);
    }

}
